package game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: liwan
 * @Description: 游戏面板
 * @Date: create in 2021/8/31 15:18:45
 */
public class GamePanel extends JPanel {
    //背景图片
    private BufferedImage bg;
    //存储多条鱼
    private List<Fish> fishList = new ArrayList<>();
    //渔网
    private Net net = new Net(this);
    //是否显示渔网
    private boolean showNet = false;
    //渔网大小
    private int netSize = 1;
    //鱼的个数
    private int num = 100;
    //分数
    private  int total = 0;

    public GamePanel() {
        this.setBackground(Color.orange);   // 设置背景颜色
        this.bg = ImageUtil.getImg("../img/bg.jpg");    //获取图片对象
        for (int i = 1; i <= num; i++) {
            fishList.add(new Fish(this));
        }
    }

    /**
     * @Author: liwan
     * @Description: 监听鼠标
     * @Params:
     * @Return:
     * @DateTime: 2021/9/2 9:43:16
     */
    public void createMouseListener() {
        //创建一个鼠标适配器
        MouseAdapter mouseAdapter = new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                net.setX(e.getX() - net.getW() / 2);
                net.setY(e.getY() - net.getH() / 2);
                repaint();
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                showNet = true;
                repaint();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                showNet = false;
                repaint();
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                switch (e.getModifiers()) {
                    case 16:
                        checkFishCatched(); // 检测鱼是否被抓住
                        repaint();
                        break;
                    case 4:
                        changeNetSize();
                        net.setX(e.getX() - net.getW() / 2);
                        net.setY(e.getY() - net.getH() / 2);
                        repaint();
                        break;
                }
            }
        };
        //绑定事件监听
        this.addMouseMotionListener(mouseAdapter);
        this.addMouseListener(mouseAdapter);
    }

    /**
    * @Author: liwan
    * @Description: 判断鱼是否在网内
    * @Params:
    * @Return:
    * @DateTime: 2021/9/2 11:49:27
    */
    private void checkFishCatched() {
        for (Fish fish : fishList) {
            boolean flag = this.net.catchFish(fish);
            if (flag && !fish.getCatched()) {
                this.total += fish.getScore();  //更新分数
                fish.setCatched(true);
            }
        }
    }

    /**
     * @Author: liwan
     * @Description: 改变渔网序号（大小）
     * @Params:
     * @Return:
     * @DateTime: 2021/9/2 10:00:12
     */
    private void changeNetSize() {
        this.netSize++;
        this.netSize = this.netSize > 7 ? 1 : this.netSize;
        this.net.changeImg(this.netSize);
    }

    /**
     * @Author: liwan
     * @Description: 启动线程
     * @Params:
     * @Return:
     * @DateTime: 2021/9/1 10:42:08
     */
    public void action() {
        for (Fish fish : fishList) {
            fish.start();
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(bg, 0, 0, GameFrame.W, GameFrame.H, null);

        //设置画笔颜色
        g.setColor(Color.CYAN);
        //设置画笔的大小
        g.setFont(new Font("楷体", Font.BOLD, 25));
        // 绘制图片
        for (Fish fish : fishList) {
            g.drawImage(fish.getImage(), fish.getX(), fish.getY(), fish.getW(), fish.getH(), null);
        }
        if (this.showNet) {
            g.drawImage(net.getImage(), net.getX(), net.getY(), net.getW(), net.getH(), null);
        }
        //绘制文本
        g.drawString("分数：" + this.total, 20, 30);
        g.drawString("子弹数：100", 490, 30);
        g.drawString("火力值：" + this.netSize, 1000, 30);
        g.drawString("鼠标右键增加火力", 955, 60);
    }
}
